package com.transcargo.action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;
import java.io.Serializable;
import java.io.IOException;

import com.transcargo.db.db;
import com.transcargo.db.dbException;
import com.transcargo.action.Action;
import com.transcargo.errors.ErrorList;


/**
 * Date: 17.02.2011
 * Time: 22:37:11
 */
public class Auth extends Action implements Serializable
{
    protected int User_Id = -1;
    protected String username = "";
    protected String password = "";
    protected Cookie cookie = null;
    protected String RemoteAddr = "";

    private void servletGetValues(HttpServletRequest request) throws ServletParametersException, ServletException {
        username = request.getParameter("login");
        password = request.getParameter("password");
        RemoteAddr = request.getRemoteAddr();
    }

    private void servletValidateValues() throws ServletValidationException {

    }
    private void servletExecuteAction() throws ServletActionException {
        try {
            db.PreStatement pre = dbase.CreateStatement("SELECT AuthUser(?, ?, ?)");
            pre.Insert(username);
            pre.Insert(password);
            pre.Insert(RemoteAddr);
            list = dbase.execute(pre);
            if (list != null) {
                iter = list.iterator();
                String[] res = iter.next();
                if (res[0].length() > 0) {
                    cookie = new Cookie("sessionid",res[0]);
                } else User_Id = -3;
            }
        } catch (java.sql.SQLException e) {
            System.out.println("SQL exception: " + e.getMessage());
            e.printStackTrace(System.out);
            throw new ServletActionException();
        } catch (dbException e) {
			System.out.println("Database exception: " + e.getMessage());
            e.printStackTrace(System.out);
			throw new ServletActionException();
		}
    }
 
    public void processAction(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		try {
            servletGetValues(request);
            servletValidateValues();
            servletExecuteAction();
            response.addCookie(cookie);
            response.sendRedirect("user.jsp");
	    } catch (ServletParametersException e) {
            if (e.getMessage() != null) {
                sendError(response, new Exception(e.getMessage()));
            } else {
                sendRedirect(response, "index.jsp");
            }

        }  catch (ServletValidationException e) {
            sendError(response, new Exception(e.getMessage()));
        }  catch (ServletActionException e) {
            sendError(response, new Exception("" + ErrorList.ERROR_STR_GENERIC  + dbase.getError()));
        } catch (IOException ee) {
			throw new ServletException("IOException: " + ee.getMessage());
		}
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        InitActionServlet(request, response);
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Content-Type", "text/html; charset=UTF-8");
        processAction(request, response);
    }
}
